function ConfirmDelete(item){
    return confirm("This will delete the selected " + item + "?");
}
function Export(exportTo){
    //alert(document.getElementById('divPrintable').innerHTML);
    document.frmExport.target="";
    document.frmExport.exportTo.value=exportTo;
    document.frmExport.txtData.value = document.getElementById('divPrintable').innerHTML;
    if(exportTo=="Print")
        document.frmExport.target="_blank";
    if(exportTo=="Email"){
        document.frmExport.target="";
    }
    document.frmExport.submit();
}
function GoBack(){
    window.history.go(-1);
}


function SetSaveAsDraft(obj){
	if(obj.checked)
		document.getElementById("divSaveAsDraft").style.display='';
	else
		document.getElementById("divSaveAsDraft").style.display='none';
}
function GetFromDraft(){
	window.open("EmailDraftPicker.aspx","maildraft","width=400, height=300"); 
   	return false;
}

function SetEmail(display){
	var divPrintableObj = document.getElementById("divPrintable");
	if(divPrintableObj){
	    if(divPrintableObj.innerHTML.length>150){
	        document.getElementById("divSendEmail").style.display=display;
	        SetSaveAsDraft(document.frmExport.chkSaveAsDraft)	;
	    }else{alert("No data to export!");}
	}
	
}
function ValidateExportDraft(){
    var bAllValid = true;
	bAllValid = CheckAllEmail(document.frmExport.txtTo,document.frmExport.txtWrongToEmail) && bAllValid;
	bAllValid = CheckAllEmail(document.frmExport.txtCC,document.frmExport.txtWrongCCEmail) && bAllValid;
	return bAllValid;
}
function ValidateManageDraft(){
    var bAllValid = true;
	bAllValid = CheckAllEmail(theForm.txtTo,theForm.txtWrongToEmail) && bAllValid;
	bAllValid = CheckAllEmail(theForm.txtCC,theForm.txtWrongCCEmail) && bAllValid;
	return bAllValid;
}
function SendEmail(){
	var bAllValid = ValidateExportDraft();
	if(bAllValid){
		SetEmail('none');
		Export('Email');
	}
	return false;
}
function CheckAllEmail(obj,objWrong){
	objWrong.style.display='none';
	var cEmails = "", wEmails = ""; 
	var emails = obj.value + ";" + objWrong.value;
	var arrEmail = emails.split(';');
	for(var i=0; i<arrEmail.length; i++){
	   var email = arrEmail[i].trim();
	   if(email.length>0){
		    if(isAlphanum(email) || validateEmail(email,false,false))
			    cEmails += email + ';';
		    else
			    wEmails += email + ';';
 	   }
	}
	obj.value = cEmails ;
	objWrong.value = wEmails;
	if(wEmails.length>0){
		objWrong.style.display='';
	 return false;
	}
	return true;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function GetEmails(target,frm){
        window.open("EmailPicker.aspx?t="+target+"&f=" + frm,"emailPicker","width=500, height=300, scrollbars=yes");
        //window.open("EmailPicker.html","a","width=400, height=200"); 
   	    return false;
    }



// Text Field Validation Functions
// copyright Stephen Chapman, 26th Dec 2004
// you may copy this function but please keep the copyright notice with it
function stripBlanks(fld) {
    var result = "";
    var c = 0;
    for (i=0; i<fld.length; i++) {
      if (fld.charAt(i) != " " || c > 0) {
        result += fld.charAt(i);
        if (fld.charAt(i) != " ") c = result.length;
        }
      }
    return result.substr(0,c);
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}
function isDecimal(parm) {return isValid(parm,numb+'.');}

function oneOnly(parm,chr,must) {
    var atPos = parm.indexOf(chr,0);
    if (atPos == -1) {return !must;}
    if (parm.indexOf(chr, atPos + 1) > - 1) {return false;}
    return true; 
}

function adjacent(parm,chrs) {
    return(parm.indexOf(chrs,0) != -1);
}

function onlyAdjacent(parm,comb,chrs) {
    var a = parm.split(comb);
    var b = a.join('');
    for (i=0; i<parm.length; i++) {
        if (val.indexOf(parm.charAt(i),0) != -1) return false;
      }
    return true; 
}

function setOrder(parm,first,second) {
    var pos1 = parmField.indexOf(first,0);
    if (pos1 == -1) return false; // first char not found
    var pos2 = parmField.indexOf(second,pos1+1);
    if (pos2 == -1) return false; // second char doesn't follow first
    return true; 
}

function setDistance(parm,first,last,min,max) {
    var pos1 = first == '' ? 0 :
    parmField.indexOf(first,0);
    var pos2 = last == '' ? parmField.length - pos1 - 1 :
    parmField.indexOf(second,pos1+1);
    if (pos1 == -1) return false;
    if (pos2 < min || pos2 > max) return false;
    return true; 
}

function endOption(fld,val) {
    return fld.substring(fld.lastIndexOf(val)) == val;
}
                  




// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateEmail(addr,man,db) {
    //alert(addr);
    if (addr == '' && man) {
       if (db) alert('email address is mandatory');
       return false;
    }
    if (addr == '') return true;
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i=0; i<invalidChars.length; i++) {
       if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
          if (db) alert('email address contains invalid characters');
          return false;
       }
    }
    for (i=0; i<addr.length; i++) {
       if (addr.charCodeAt(i)>127) {
          if (db) alert("email address contains non ascii characters.");
          return false;
       }
    }

    var atPos = addr.indexOf('@',0);
    if (atPos == -1) {
       if (db) alert('email address must contain an @');
       return false;
    }
    if (atPos == 0) {
       if (db) alert('email address must not start with @');
       return false;
    }
    if (addr.indexOf('@', atPos + 1) > - 1) {
       if (db) alert('email address must contain only one @');
       return false;
    }
    if (addr.indexOf('.', atPos) == -1) {
       if (db) alert('email address must contain a period in the domain name');
       return false;
    }
    if (addr.indexOf('@.',0) != -1) {
       if (db) alert('period must not immediately follow @ in email address');
       return false;
    }
    if (addr.indexOf('.@',0) != -1){
       if (db) alert('period must not immediately precede @ in email address');
       return false;
    }
    if (addr.indexOf('..',0) != -1) {
       if (db) alert('two periods must not be adjacent in email address');
       return false;
    }
    var suffix = addr.substring(addr.lastIndexOf('.')+1);
    if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
       if (db) alert('invalid primary domain in email address');
       return false;
    }
    return true;
}